import React from 'react'
import { useDispatch } from 'react-redux'
// 引入actionCreator
import { addTodo, asyncTodo } from '../../redux/store'
import './Header.css'
export default function Header() {
  const dispatch = useDispatch()
  return (
    <div className="todo-header">
      <input
        type="text"
        onKeyDown={(e) => {
          const value = e.target.value.trim()
          if (e.key !== 'Enter' || !value) return
          // 同步添加
          // dispatch(addTodo(value))
          // 异步添加
          dispatch(asyncTodo(value))
          e.target.value = ''
        }}
      />
    </div>
  )
}
